// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#include "coin2/exchange/okex_v5/api_order/native_rest_async_asio_subscriber.h"

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>

#include "coin2/base/log.h"

namespace coin2::exchange::okex_v5::api_order {

void OkexV5RestAsyncAsioSubscriber::QueryBalance() {
  if (is_readonly_order_system_ && symcache_->is_exwallet()) {
    QueryFundBalance();
    return;
  }
  auto http_context = rest_client()->QueryAccountInfo();
  if (!http_context) return;
  http_context->handler =
      std::bind(&OkexV5RestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void OkexV5RestAsyncAsioSubscriber::QueryFundBalance() {
  auto http_context = rest_client()->QueryFundBalance();
  if (!http_context) return;
  http_context->handler =
      std::bind(&OkexV5RestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void OkexV5RestAsyncAsioSubscriber::QueryOpenOrders(std::string_view after_order_id) {
  auto http_context = rest_client()->QueryOpenOrders("SPOT", after_order_id);
  if (!http_context) return;
  http_context->handler =
      std::bind(&OkexV5RestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void OkexV5RestAsyncAsioSubscriber::QueryFinishedOrders() {
  auto http_context = rest_client()->QueryHistoryOrders("SPOT");
  if (!http_context) return;
  http_context->handler =
      std::bind(&OkexV5RestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void OkexV5RestAsyncAsioSubscriber::QueryFills() {
  auto http_context = rest_client()->QueryFills("SPOT");
  if (!http_context) return;
  http_context->handler =
      std::bind(&OkexV5RestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void OkexV5RestAsyncAsioSubscriber::QueryTransfer() {
  RestParams params;
  auto http_context = rest_client()->QueryDeposit(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&OkexV5RestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);

  http_context = rest_client()->QueryWithdrawal(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&OkexV5RestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);

  http_context = rest_client()->QueryTransfer(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&OkexV5RestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void OkexV5RestAsyncAsioSubscriber::SubscribeAsync() {
  AddPeriodicTask(
      std::bind(&OkexV5RestAsyncAsioSubscriber::QueryBalance, this),
      config_.query_account_balance_period_sec(),
      "query_balance");

  if (is_readonly_order_system_) {
    AddPeriodicTask(
        std::bind(&OkexV5RestAsyncAsioSubscriber::QueryTransfer, this),
        config_.query_account_balance_period_sec(),
        "query_transfer");
  }

  AddPeriodicTask(
      std::bind(&OkexV5RestAsyncAsioSubscriber::QueryOpenOrders, this, ""),
      config_.query_open_orders_period_sec(),
      "query_open_orders");

  AddPeriodicTask(
      std::bind(&OkexV5RestAsyncAsioSubscriber::QueryFinishedOrders, this),
      config_.query_cancel_orders_period_sec(),
      "query_finished_orders");

  AddPeriodicTask(
      std::bind(&OkexV5RestAsyncAsioSubscriber::QueryFills, this),
      config_.query_fill_period_sec(),
      "query_fills");

  // handle number of open order more than 100
  mb_->Subscribe("okex_spot/query_open_orders", [this](const std::string&, void* data) {
    std::string_view after_order_id = *(reinterpret_cast<std::string*>(data));
    this->QueryOpenOrders(after_order_id);
  });
}

}  // namespace coin2::exchange::okex_v5::api_order
